<!--  -->
<template>
  <div class="">
    <h2>品牌管理</h2>
    <!-- <div id="search-card">
      <div id="search-content">
        <el-form v-model="search" :inline="true">

          <el-form-item label="品牌名称">
            <el-input v-model="search.orgName" />
          </el-form-item>

          <el-form-item label="品牌分类">
            <el-select v-model="search.orgType">
              <el-option
                v-for="otype in orgT"
                :key="otype.id"
                :value="otype.id"
                :label="otype.typeName"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="负责人">
            <el-input v-model="search.head" />
          </el-form-item>

          <el-form-item label="联系人">
            <el-input v-model="search.contacts" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="conditionQuery">查找</el-button>
            <el-button icon="el-icon-refresh" circle @click="fetchData()" />
          </el-form-item>

        </el-form>
      </div>

    </div> -->

    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <!-- <el-table-column
          align="center"
          prop="orgId"
          label="品牌编号"
          min-width="10%"
        /> -->

        <el-table-column
          label="序号"
          type="index"
          min-width="8%"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="orgName"
          label="品牌名称"
          min-width="20%"
        />

        <el-table-column
          align="center"
          prop="typeName"
          label="行业分类"
          min-width="10%"
        />

        <el-table-column
          align="center"
          prop="consoleUser.username"
          label="负责人"
          min-width="8%"
        />

        <el-table-column
          align="center"
          prop="contactTel"
          label="品牌联系电话"
          min-width="13%"
        />

        <el-table-column
          align="center"
          prop="contacts"
          label="联系人"
          min-width="8%"
        />

        <el-table-column
          align="center"
          prop="contactNumber"
          label="联系人电话"
          min-width="13%"
        />

        <el-table-column align="center" label="操作" min-width="20%">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpen(Scope.row.orgId)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="visible" @close="handleClose">
      <el-form ref="org" :model="org" label-position="top">
        <!-- <el-form-item label="品牌编号">
          <el-input v-model="org.orgId" autocomplete="off" readonly="" />
        </el-form-item> -->

        <el-form-item prop="orgName" label="品牌名称">
          <el-input v-model="org.orgName" autocomplete="off" maxlength="50" show-word-limit readonly />
        </el-form-item>
        <el-form-item prop="orgType" label="行业分类">
          <!-- <el-input v-model="org.orgType" autocomplete="off" /> -->
          <el-select v-model="org.orgType" placeholder="请选择" disabled>
            <el-option
              v-for="otype in orgT"
              :key="otype.id"
              :value="otype.id"
              :label="otype.typeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="品牌图片">

          <el-upload
            class="avatar-uploader"
            name="files"
            :action="url"
            :show-file-list="false"
            disabled
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
          <!-- <img v-if="type==2" :src="org.orgImg" width="200px" height="120px"> -->
        </el-form-item>
        <el-form-item label="品牌简介">
          <!-- <editor :cur-value="org.orgInfo" @input="newContent" /> -->
          <p v-html="org.orgInfo" />
        </el-form-item>
        <el-form-item prop="head" label="负责人">
          <!-- <el-input v-model="org.head" autocomplete="off" /> -->
          <el-select v-model="org.head" placeholder="请选择" disabled>
            <el-option
              v-for="ohead in head"
              :key="ohead.id"
              :value="ohead.id"
              :label="ohead.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="contactTel" label="联系电话(座机)">
          <el-input v-model="org.contactTel" autocomplete="off" maxlength="13" readonly />
        </el-form-item>
        <el-form-item prop="depositBank" label="银行">
          <!-- <el-input v-model="org.depositBank" autocomplete="off" /> -->
          <el-autocomplete
            v-model="org.depositBank"
            class="inline-input"
            :fetch-suggestions="querySearch"
            :popper-append-to-body="false"
            popper-class="select-option"
            placeholder="请输入内容"
            readonly
            @select="handleSelect"
          />
        </el-form-item>
        <el-form-item prop="account" label="银行账号">
          <el-input v-model="org.account" autocomplete="off" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" maxlength="30" readonly />
        </el-form-item>
        <el-form-item prop="contacts" label="联系人">
          <el-input v-model="org.contacts" autocomplete="off" maxlength="20" readonly />
        </el-form-item>
        <el-form-item prop="contactNumber" label="联系人电话">
          <el-input v-model="org.contactNumber" autocomplete="off" maxlength="11" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" readonly />
        </el-form-item>
        <el-form-item label="地区">
          <!-- <el-input v-model="org.orgAddress" autocomplete="off" /> -->
          <el-cascader
            v-model="selectedOptions"
            size="large"
            :options="options"
            disabled
            @change="handleChange"
          />
        </el-form-item>

        <el-form-item label="营业时间">
          <el-tooltip class="item" effect="dark" content="例：8:00~20:00" placement="bottom-start">
            <el-input v-model="org.orgTime" autocomplete="off" placeholder="例：8:00~20:00" maxlength="20" readonly />
          </el-tooltip>

        </el-form-item>
        <el-form-item prop="orgEmail" label="品牌邮箱">
          <el-input v-model="org.orgEmail" autocomplete="off" maxlength="32" readonly />
        </el-form-item>
        <el-form-item prop="shareRatio" label="分账比例">
          <el-input v-model="org.shareRatio" autocomplete="off" maxlength="10" readonly />
        </el-form-item>
        <el-form-item label="推荐人">
          <!-- <el-input v-model="org.referenceId" autocomplete="off" /> -->
          <el-select v-model="org.referenceId" placeholder="请选择" disabled>
            <el-option
              v-for="oreference in reference"
              :key="oreference.id"
              :value="oreference.id"
              :label="oreference.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="推荐人分成">
          <el-input v-model="org.referenceRatio" autocomplete="off" maxlength="10" readonly />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">返 回</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getOrgByRe, conditionSearch, mohuSearch, getOrgById } from '@/api/org'
import { getAllType } from '@/api/classType'
import { getUserByGroup, getUserById } from '@/api/consoleUser'
import { regionData, CodeToText, TextToCode } from 'element-china-area-data'
export default {
  name: 'Org',
  // import引入的组件需要注入到对象中才能使用
  components: {
  },
  data() {
    // 这里存放数据
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      listLoading: false,
      visible: false,
      org: {},
      title: '查看品牌详情',
      search: {},
      mohu: '',
      orgT: {},
      text: '',
      url: process.env.VUE_APP_BASE_API + 'file/',
      imageUrl: '',
      options: regionData,
      selectedOptions: [],
      areaList: {},
      area: '',
      banks: [],
      head: {},
      reference: {}
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
    this.getOrgType()
    this.getAllHead()
    this.getAllReference()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.search = {}
      getOrgByRe().then((resp) => {
        this.list = resp.obj
        console.log(this.list)
      })
      this.listLoading = false
    },
    handleOpen(id) {
      this.visible = true
      this.getAllHead()
      getOrgById(id).then((resp) => {
        this.org = resp.obj
        this.imageUrl = resp.obj.orgImg
        this.area = this.org.orgAddress
        this.jiema()
        getUserById(this.org.head).then((resp) => {
          this.head.push(resp.obj)
          console.log(this.head)
        })
      })
    },
    getOrgType() {
      getAllType().then((resp) => {
        this.orgT = resp.obj
      })
    },
    handleClose() {
      this.visible = false
    },
    conditionQuery() {
      console.log(this.search)
      conditionSearch(this.search).then((data) => {
        this.list = data.obj
        this.currentPage = 1
      })
    },
    mohuQuery() {
      console.log(this.mohu)
      mohuSearch(this.mohu).then((data) => {
        this.list = data.obj
        this.currentPage = 1
      })
    },

    handleChange(value) {
      console.log(this.selectedOptions)
      this.area = CodeToText[value[0]] + ';' + CodeToText[value[1]] + ';' + CodeToText[value[2]]
      console.log(this.area)
    },
    jiema() {
      console.log(this.area)
      this.areaList = this.area.split(';')
      console.log(this.areaList)
      this.selectedOptions = [TextToCode[this.areaList[0]].code, TextToCode[this.areaList[0]][this.areaList[1]].code, TextToCode[this.areaList[0]][this.areaList[1]][this.areaList[2]].code]
    },
    querySearch(queryString, cb) {
      var banks = this.banks
      var results = queryString ? banks.filter(this.createFilter(queryString)) : banks
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return (banks) => {
        return (banks.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    handleSelect(item) {
      console.log(item)
      this.org.depositBank = item.value
    },
    getAllHead() {
      getUserByGroup(3).then((resp) => {
        this.head = resp.obj
      })
    },
    getAllReference() {
      getUserByGroup(2).then((resp) => {
        this.reference = resp.obj
      })
    },
    isCard(val) {
      if (!/^([1-9]{1})(\d{15}|\d{18})$/.test(val)) {
        return false
      } else {
        return true
      }
    },
    isCellPhone(val) {
      if (!/^1[3456789]\d{9}$/.test(val)) {
        return false
      } else {
        return true
      }
    },
    isChinese(val) {
      if (/[^\u4E00-\u9FA5]/.test(val)) {
        return false
      } else {
        return true
      }
    },
    // 特殊字符过滤
    checkSpecificKey(str) {
      var specialKey = "[`~!#$^&*()=|{}':;',\\[\\].<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘'"
      for (var i = 0; i < str.length; i++) {
        if (specialKey.indexOf(str.substr(i, 1)) !== -1) {
          return false
        }
      }
      return true
    },

    // 验证字符串
    validateFontSize(rule, value, callback) {
      if (value.length < 30) {
        callback()
      } else {
        callback(new Error('字符串长度在1-30之间'))
      }
    },

    // 验证字符串
    validateCharacter(rule, value, callback) {
      if (this.checkSpecificKey(value)) {
        callback()
      } else {
        callback(new Error('请不要输入特殊字符'))
      }
    },
    checkRtelePhone(rule, value, callback) {
      if (!value) {
        return callback(new Error('联系电话不能为空'))
      } else {
        const reg = /^(0\d{2,3}-?)?[0-9]{7,8}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的联系电话'))
        }
      }
    },
    checkEmail(rule, value, callback) {
      if (!value) {
        return callback(new Error('电子邮箱不能为空'))
      } else {
        const reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入有效的电子邮箱！'))
        }
      }
    },
    newContent(val) {
      console.log(val) // 获取输入内容
      this.org.orgInfo = val
    }
  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped lang="less">
.el-cascader{
  width: 400px;
}
.el-autocomplete{
  width: 400px;
}
.el-popper[x-placement^=bottom]{
		width:400px !important;
	}
</style>
<style  scoped>
#main{
  margin-left: 5px;
  margin-right: 5px;
}
#table-area{
  margin-left: 10px;
  margin-right: 10px;
}
#search-card {
  line-height: 0px;
        text-align: right;
        width: 100%;
        height: 70px;
        overflow-x: hidden;
}
.avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
    .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 360px;
      height: 150px;
      line-height: 150px;
      text-align: center;
    }
    .avatar {
      width: 360px;
      height: 150px;
      display: block;
    }
</style>
